package com.ttc.ssm.service.admin.impl;

import com.ttc.ssm.common.CompanyPage;
import com.ttc.ssm.common.Page;
import com.ttc.ssm.controller.admin.CompanyController;
import com.ttc.ssm.mapper.admin.*;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiarui on 2016/7/25.
 */
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    CompanyDetailMapper companyDetailMapper;

    @Autowired
    ZoneMapper zoneMapper;

    @Autowired
    IndustryMapper industryMapper;

    @Autowired
    CompanySizeMapper companySizeMapper;

    @Autowired
    CompanyPropertyMapper companyPropertyMapper;

    @Autowired
    CityMapper cityMapper;

    @Override
    public List<Company> selectCompanyForPage(CompanyPage page) throws Exception {
        List<Company> companies =  companyMapper.selectCompanyForPage(page);
        return companies;
    }

    @Override
    public List<Company> selectVerifyForPage(CompanyPage page) {
        List<Company> companies =  companyMapper.selectVerifyForPage(page);
        return companies;
    }

    @Override
    public void deleteCompanies(Map<String, Object> map, HttpServletRequest request) throws Exception {
        List<CompanyDetail> companyDetails = companyDetailMapper.selectDetails(map);
        for(CompanyDetail companyDetail:companyDetails){
            if(!(companyDetail.getLicence_url()==null))
                CompanyController.deleteLicence(companyDetail,request);
        }
        companyMapper.deleteCompanies(map);
        companyDetailMapper.deleteCompaniesDetail(map);
    }

    @Override
    public void updateCompanyStatus(int company_id, int status) throws Exception {
        Date d = new Date();
        String s;
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        s = format1.format(d);
        companyMapper.updateCompanyStatus(company_id,status,s);
    }

    @Override
    public CompanyDetail selectCompanyDetail(int company_id) throws Exception {
        CompanyDetail companyDetail = companyDetailMapper.selectCompanyDetail(company_id);
        return companyDetail;
    }

    @Override
    public Company selectCompanyById(int company_id) throws Exception {
        Company company = companyMapper.selectCompanyById(company_id);
        return company;
    }

    @Override
    public Industry select_industry_name(int industry_id) throws Exception {
        Industry industry = companyDetailMapper.select_industry_name(industry_id);
        return industry;
    }

    @Override
    public List<Zone> getZone() throws Exception {
        return zoneMapper.getZone();
    }

    @Override
    public List<Industry> getIndustry() throws Exception {
        return industryMapper.getIndustry();
    }

    @Override
    public List<CompanySize> getCompanySize() throws Exception {
        return companySizeMapper.getCompanySize();
    }

    @Override
    public List<CompanyProperty> getCompanyProperty() throws Exception {
        return companyPropertyMapper.getCompanyProperty();
    }

    @Override
    public List<City> getCityByZoneId(int zone_id) throws Exception {
        return cityMapper.getCityByZoneId(zone_id);
    }

    @Override
    public void updateCompany(Company company) throws Exception {
        companyMapper.updateCompany(company);
    }

    @Override
    public void updateCompanyDetail(CompanyDetail companyDetail) throws Exception {
        companyDetailMapper.updateCompanyDetail(companyDetail);
    }

    @Override
    public void insertCompanyDetail(CompanyDetail companyDetail) throws Exception {
        companyDetailMapper.insertCompanyDetail(companyDetail);
    }

    @Override
    public void verifyCompanyDetail(int status, int company_id) throws Exception {
        Date d = new Date();
        companyDetailMapper.verifyCompanyDetail(status,d,company_id);
    }

    @Override
    public int countRows(CompanyPage companyPage) throws Exception {
        return companyMapper.countRows(companyPage);
    }

    @Override
    public int countRowsVerify(CompanyPage companyPage) throws Exception {
        return companyMapper.countRowsVerify(companyPage);
    }

}
